<?php

namespace app\mini\controller;
use think\App;
use think\facade\Session;
class QyWechatController extends Access
{

    public function initialize()
    {
        parent::initialize();
//        $this->handleInit();
        $this->agentId =env('weChat.AgentId','');
        $this->Secret =env('weChat.Secret','');
        $this->corpid =env('weChat.corpid','');
    }
    public function index($action,$params=[])
    {
        $actions = [
            'setReservationAction',//通知被预约员工
            'cancelReservationAction',//通知员工预约被取消
        ];
        if(!in_array($action,$actions))
        {
            return false;
        }

        if(Session::has('access_token'))
        {
            $access_token=Session::get('access_token');
        }else{
            $access_token = $this->setAccessToken();
            if(!$access_token)
            {
                return [
                    'code'=>400001,
                    'msg'=>"获取token失败"
                ];
            }
            Session::set('access_token',$access_token);
        }

        // return $access_token;
        switch ($action)
        {
            case 'setReservationAction':{
                $url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserid?access_token={$access_token}";
                $param=[
                    'mobile'=>$params['mobile']
                ];
                $result = $this->httpPost($url,$param);

                // return $result;
                // break;
                if($result['errcode']!=0)
                {
                    return false;
                }
                $url="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={$access_token}";
                $param=[
                    'touser'=>$result['userid'],
                    'msgtype'=>'text',
                    'agentid'=>$this->agentId,
                    'toparty'=>"",
                    'totag'=>"",
                    'text'=>[
                        'content'=>"【你有新的预约客户】<br>客户「{$params['customer_name']}」预约了{$params['start']}-{$params['end']}"
                    ],
                ];
                $result = $this->httpPost($url,$param);

                return $result;
                break;

            }
            case 'cancelReservationAction':{
                $url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserid?access_token={$access_token}";
                $param=[
                    'mobile'=>$params['mobile']
//                    'mobile'=>"17637872671"
                ];
                $result = $this->httpPost($url,$param);
                if($result['errcode']!=0)
                {
                    return false;
                }
                $url="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={$access_token}";
                $param=[
                    'touser'=>$result['userid'],
                    'msgtype'=>'text',
                    'agentid'=>$this->agentId,
                    'toparty'=>"",
                    'totag'=>"",
                    'text'=>[
                        'content'=>"【客户取消预约】<br>客户「{$params['customer_name']}」取消了{$params['start']}{$params['end']}时段的预约"
                    ],
                ];
                $result = $this->httpPost($url,$param);

                return $result;
                break;

            }
        }

    }
    public function setReservationAction()
    {
        $result = $this->index("setReservationAction",[
            'mobile'=>17637872671,
            'start'=>'2023'.'-'.'9-5'.' '.'16:30',
            'end'=>"17:30",
            'customer_name'=>"段文朋"
        ]);
        return $this->success($result);
    }
    public function setAccessToken()
    {
        $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={$this->corpid}&corpsecret={$this->Secret}";
        $curl = curl_get($url);
        $data = json_decode($curl,true);
        if($data['errcode']!=0)
        {
            return false;
        }else{
            return $data['access_token'];
        }
    }
    public function httpPost($url,$data)
    {
        $data = json_encode($data);
        $curl = curl_init();
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl,CURLOPT_POST,true);
        curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
        $result = curl_exec($curl);
        curl_close($curl);
        return json_decode($result,true);
    }
    public function httpGet($url,$data)
    {
        $url .='?'.http_build_query($data);
        $curl = curl_init();
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
        $result = curl_exec($curl);
        curl_close($curl);
        return json_decode($result,true);

    }

}
